package cn.icanci.learn.v1.aop3;

import cn.icanci.learn.v1.aop.Animal;
import cn.icanci.learn.v1.aop.Cat;

import java.lang.reflect.InvocationTargetException;

/**
 * @author icanci
 * @date Created in 2021/06/27 17:08
 * @version 1.0
 */
public class AnimalMainTest3 {
    public static void main(String[] args) {
        Animal cat = new Cat();
        // 创建责任链
        final AbstractHandler.HeadHandler headHandler = new AbstractHandler.HeadHandler();
        final OneHandler oneHandler = new OneHandler();
        oneHandler.setNext(new TwoHandler());

        headHandler.setNext(oneHandler);
        // 创建代理对象
        final JDKDynamicProxy3 jdkDynamicProxy3 = new JDKDynamicProxy3(cat, headHandler);
        final Animal proxy = (Animal) jdkDynamicProxy3.getProxy();
        proxy.eat();
    }

    public static class OneHandler extends AbstractHandler {

        @Override
        Object invoke(TargetMethod targetMethod) throws InvocationTargetException, IllegalAccessException {
            System.out.println("one handler begin");
            final Object proceed = proceed(targetMethod);
            System.out.println("one handler end");
            return proceed;
        }
    }

    public static class TwoHandler extends AbstractHandler {

        @Override
        Object invoke(TargetMethod targetMethod) throws InvocationTargetException, IllegalAccessException {
            System.out.println("two handler begin");
            final Object proceed = proceed(targetMethod);
            System.out.println("two handler end");
            return proceed;
        }
    }
}
